package com.android.lovely.ui.dialog

import android.icu.text.DecimalFormat
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.android.lovely.R
import com.android.lovely.databinding.DialogChatGemsBinding
import com.android.lovely.framework.BaseDialogFragment
import com.android.lovely.framework.UIController.showToast
import com.android.lovely.ui.chat.ConversationViewModel
import com.android.lovely.ui.chat.getChatId
import com.android.lovely.user.UserManager
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.dimen.Dimension.dp
import com.kotlin.extensions.view.ViewExt.clickable
import io.glassfy.androidsdk.GooglePlayManager
import io.glassfy.androidsdk.model.PurchaseParams

class ChatGemDialog : BaseDialogFragment<DialogChatGemsBinding>() {
    private val viewModel by activityViewModels<ConversationViewModel> {
        viewModelFactory {
            initializer {
                ConversationViewModel(getChatId())
            }
        }
    }

    override fun initializeViews() {

        val product = viewModel.character.product
        if (product == null) {
            dismiss()
            return
        }
        val count = product.point
        binding.price.text = "\$${DecimalFormat("0.##").format(product.price)}"
        binding.oldPrice.text = "\$${DecimalFormat("0.##").format(product.oldPrice)}"
        binding.oldPrice.paint.isStrikeThruText = true
        count.toString().forEach {
            val imageView = ImageView(context)
            imageView.layoutParams = LinearLayout.LayoutParams(-2, -1).apply {
                marginEnd = 3.dp
            }
            imageView.adjustViewBounds = true
            imageView.setImageResource(it.toString().toInt().getImageByNum())
            binding.count.addView(imageView)
        }
        binding.get.clickable {
            lifecycleScope.launchSafety{
                val result = GooglePlayManager.purchase(
                    requireActivity(),
                    product.appleId,
                    PurchaseParams(
                        UserManager.userInfo?.id?.toString() ?: return@launchSafety,
                        product.price.toFloat()
                    )
                )
                if (!result) {
                    showToast(getString(R.string.pay_failed))
                } else {
                    viewModel.updateFeatureStatus()
                    dismissAllowingStateLoss()
                }
            }
        }
    }

    private fun Int.getImageByNum(): Int {
        return when (this) {
            0 -> R.drawable.chat_pay0
            1 -> R.drawable.chat_pay1
            2 -> R.drawable.chat_pay2
            3 -> R.drawable.chat_pay3
            4 -> R.drawable.chat_pay4
            5 -> R.drawable.chat_pay5
            6 -> R.drawable.chat_pay6
            7 -> R.drawable.chat_pay7
            8 -> R.drawable.chat_pay8
            9 -> R.drawable.chat_pay9
            else -> R.drawable.chat_pay0
        }
    }
}